forval i = 1/3{
	
	forval r = 0/1{
	import delimited "$dir/Model/simulated_data/simulated_data_cfact_`i'_pbr_1_rec_`r'.csv", clear
	
	//renaming
	ren v1 theta
	ren v2 e
	ren v3 x
	ren v4 a
	ren v5 t
	ren v6 p
	ren v7 I
	ren v8 H 
	ren v9 tau
	ren v10 s
	ren v11 s_val
	ren v12 prob_diff_I
	ren v13 prob_diff_H
	ren v14 l 
	ren v15 m
	ren v16 w
	ren v17 tax
	ren v18 c
	ren v19 util
	ren v20 switch
	ren v21 gov_exp
	ren v22 sim
	ren v23 i_v

	
	drop if t == 0 

	//get lifetime wages and years worked for each individual simulation
	preserve
	collapse (sum) w l tax, by(i_v sim)
	tempfile outcomes
	ren w w_total
	ren l l_total
	ren tax tax_total
	save `outcomes'
	restore

	merge m:1 i_v sim using `outcomes', keep(match) nogen

	keep if t == 23 
	gen enroll = (s>1)
	gen hs = (e==1 & s==1)
	gen drop = (e==1 & s>1)
	gen sc = (e==2)
	gen coll = (e==3)

	su enroll hs drop sc coll w_total l_total tax_total
	gen inc_dec = ceil(i_v / 2)


	//lifetime wages and years worked, average savings and educadtional attainment, by parent income ventile
	collapse (mean) w_total l_total tax_total enroll hs drop sc coll gov_exp, by (inc_dec)
	ds inc_dec, not
	foreach var in `r(varlist)'{
		ren `var' `var'_cfact_`i'_rec_`r'
	}
	save "$temp/outcomes_cfact_`i'_rec_`r'", replace
	}
}

//get to work!
use "$temp/outcomes_base_pinc_dec", clear
merge 1:1 inc_dec using "$temp/outcomes_rec_pinc_dec", keep(match) nogen

ds w_total l_total tax_total enroll hs drop sc coll gov_exp
foreach var in `r(varlist)'{
	gen `var'_delta_rec = `var'_rec - `var'
}
replace coll_delta_rec = coll_delta_rec * 100
replace w_total_delta_rec = w_total_delta_rec * 40000
replace w_total_delta_rec = w_total_delta_rec / 1000


//recession impacts under counterfactual financial aid schemes
forval c = 1/3{
	forval r = 0/1{
		merge 1:1 inc_dec using "$temp/outcomes_cfact_`c'_rec_`r'", keep(match) nogen
	}
	gen coll_delta_rec_cfact_`c' = coll_cfact_`c'_rec_1 - coll_cfact_`c'_rec_0
	gen w_total_delta_rec_cfact_`c' = w_total_cfact_`c'_rec_1 - w_total_cfact_`c'_rec_0

	replace coll_delta_rec_cfact_`c' = coll_delta_rec_cfact_`c' * 100
	replace w_total_delta_rec_cfact_`c' = w_total_delta_rec_cfact_`c' * 40000
	replace w_total_delta_rec_cfact_`c' = w_total_delta_rec_cfact_`c' / 1000
}

save "$temp/rec_impacts_pells", replace


twoway connected coll_delta_rec inc_dec, ylabel(-5(2.5)15) yline(0) xlabel(1(1)10) color(black) msize(medsmall) msymbol(circle) ///
|| connected coll_delta_rec_cfact_1 inc_dec, color(black) msize(medsmall) msymbol(triangle) lp(dash) ///
|| connected coll_delta_rec_cfact_2 inc_dec, color(black) msize(medsmall) msymbol(square) lp(dot) ///
|| connected coll_delta_rec_cfact_3 inc_dec, color(black) msize(medsmall) msymbol(diamond_hollow) lp(dash_dot) ///
legend(lab(1 "Baseline") lab(2 "Expanded Flat Transfer") lab(3 "Expanded Progressivity") lab(4 "Expanded Wealth Component")) xtitle("Parent Income Decile") ytitle("Change in College Attainment (pp)") 
graph export "$output/recession_impacts_coll_pells.png", replace


twoway connected w_total_delta_rec inc_dec, ylabel(-50(25)100) yline(0) xlabel(1(1)10) color(black) msize(medsmall) msymbol(circle) ///
|| connected w_total_delta_rec_cfact_1 inc_dec, color(black) msize(medsmall) msymbol(triangle) lp(dash) ///
|| connected w_total_delta_rec_cfact_2 inc_dec, color(black) msize(medsmall) msymbol(square) lp(dot) ///
|| connected w_total_delta_rec_cfact_3 inc_dec, color(black) msize(medsmall) msymbol(diamond_hollow) lp(dash_dot) ///
legend(lab(1 "Baseline") lab(2 "Expanded Flat Transfer") lab(3 "Expanded Progressivity") lab(4 "Expanded Wealth Component")) xtitle("Parent Income Decile") ytitle("Change in Lifetime Earnings ($1000)") 
graph export "$output/recession_impacts_w_pells.png", replace
graph close





